﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Linq;
using System.Text;
using Project362.Files;

namespace Project362.Collections
{
    public class CollectionHandler
    {
        private Dictionary<string, Collection> dir;
        private string dictionaryFile;
        /// <summary>
        /// Initializes a new collectionhandler for managing collection objects
        /// </summary>
        /// <param name="filename">Location to store the saved database</param>
        public CollectionHandler(string filename)
        {
            this.dictionaryFile=filename;
            if (File.Exists(dictionaryFile))
            {
                try
                {
                    Stream stream = File.Open(dictionaryFile, FileMode.Open);
                    BinaryFormatter bf = new BinaryFormatter();
                    dir = (Dictionary<string, Collection>)bf.Deserialize(stream);
                    stream.Close();
                }
                catch (Exception e)
                {
                    throw e; //TODO
                }
            }
            else
            {
                dir = new Dictionary<string, Collection>();
            }
        }
        /// <summary>
        /// Adds a file to a collection
        /// </summary>
        /// <param name="filename">Filename to add</param>
        /// <param name="collection">Collection to add to</param>
        /// <returns>success or failure</returns>
        public bool addFileToCollection(string filename, string collection)
        {
            if (!dir.ContainsKey(collection)) { return false; }
            else
            {
                return dir[collection].addFile(filename);
            }
        }
        /// <summary>
        /// Removes a file from a given collection
        /// </summary>
        /// <param name="filename">Filename to remove</param>
        /// <param name="collection">Collection to remove from</param>
        /// <returns>Success or failure</returns>
        public bool removeFileFromCollection(string filename, string collection)
        {
            if (!dir.ContainsKey(collection)) { return false; }
            else
            {
                return dir[collection].removeFile(filename);
            }
        }
        /// <summary>
        /// Get files stored in a particular collection
        /// </summary>
        /// <param name="collection">The collection to get files from</param>
        /// <returns>A list of fileclass objects</returns>
        public List<FileClass> getFilesInCollection(string collection)
        {
            if (!dir.ContainsKey(collection)) { return null; }
            else
            {
                return dir[collection].getFiles();
            }
        }
        /// <summary>
        /// Moves a file from one collection to another
        /// </summary>
        /// <param name="filename">The file to move</param>
        /// <param name="sourcec">The collection the file is currently in</param>
        /// <param name="destc">The collection to move the file to</param>
        /// <returns>Success or failure</returns>
        public bool moveFileBetweenCollections(string filename, string sourcec, string destc)
        {
            if (!dir.ContainsKey(sourcec) || !dir.ContainsKey(destc)) { return false; }
            else
            {
                if (!dir[sourcec].contains(filename) || dir[destc].contains(filename)) { return false; }
                else
                {
                    if (sourcec == destc) { return true; }
                    else
                    {
                        bool ret = dir[destc].addFile(dir[sourcec].getFile(filename));
                        ret = ret & dir[sourcec].removeFile(filename);
                        return ret;
                    }
                }
            }
        }
        /// <summary>
        /// Adds a new collection
        /// </summary>
        /// <param name="collection">Name of new collection</param>
        /// <returns>success or failure</returns>
        public bool addNewCollection(string collection)
        {
            if (dir.ContainsKey(collection)) { return false; }
            else
            {
                Collection newCollection = new Collection();
                dir.Add(collection, newCollection);
                return true;
            }
        }

        /// <summary>
        /// Removes an existing collection
        /// </summary>
        /// <param name="collection">Collection to remove</param>
        /// <returns>success or failure</returns>
        public bool removeCollection(string collection)
        {
            if (!dir.ContainsKey(collection)) { return false; }
            else
            {
                dir.Remove(collection);
                return true;
            }
        }
        /// <summary>
        /// Save collections to disk
        /// </summary>
        /// <returns>success or failure</returns>
        public bool saveCollections()
        {
            try
            {
                Stream stream = File.Open(dictionaryFile, FileMode.Create);
                BinaryFormatter bf = new BinaryFormatter();
                bf.Serialize(stream, dir);
                stream.Close();
            }
            catch (Exception e)
            {
                return false;
            }
            return true;
        }
    }
}
